package invs.invs300both;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.invs.InvsIdCard;
import com.invs.invswlt;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class CardRecDtlActivity extends Activity implements OnClickListener {

	CardRecordDbHelper m_Recdb = null; 
	Cursor m_RecCursor = null;
	int m_iTotalRec = 0;
	int m_iRowNum = 0;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_card_dtl); 
        
        Button btn = (Button)findViewById(R.id.firstpage);
        btn.setOnClickListener(this); 
        btn = (Button)findViewById(R.id.prepage);
        btn.setOnClickListener(this);  
        btn = (Button)findViewById(R.id.nextpage);
        btn.setOnClickListener(this);          
        btn = (Button)findViewById(R.id.lastpage);
        btn.setOnClickListener(this); 
        
        m_Recdb = new CardRecordDbHelper(this);
        
        Bundle bundle = this.getIntent().getExtras();
        m_iTotalRec = bundle.getInt("total");
        ShowRecord(bundle.getInt("rownum"));
        
        EditText edit = (EditText)findViewById(R.id.curpage);
        edit.setOnKeyListener(new OnKeyListener() {			
			public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
				if(arg1 == KeyEvent.KEYCODE_ENTER) {				
					EditText edit = (EditText)findViewById(R.id.curpage);
					ShowRecord(Integer.parseInt(edit.getText().toString()));
				}
				return false;
			}
		});    
    };
    
    public void onClick(View arg0) {
		switch(arg0.getId()){
		case R.id.firstpage:	
			ShowRecord(1);
			break;		
		case R.id.prepage:	
			PreRow();
			break;
		case R.id.nextpage:
			NextRow();
			break;	
		case R.id.lastpage:	
			ShowRecord(m_iTotalRec);
			break;			
		}
	} 	
    
    void PreRow() {
		ShowRecord(m_iRowNum-1);
	}
		
	void NextRow() {
		ShowRecord(m_iRowNum+1);
	}
	
	void ShowRecord(int iRowNum) {
        if (m_RecCursor != null)
        	m_RecCursor.close();
        
        if (m_iTotalRec == 0){
        	return;
        }
        
        if (iRowNum > m_iTotalRec){
        	iRowNum = m_iTotalRec;
        }
        
        if (iRowNum <1){
        	iRowNum = 1;
        }
        
        m_iRowNum = iRowNum;
        m_RecCursor = m_Recdb.GetRow(m_iRowNum);  

		Bundle bundle = new Bundle();

		m_RecCursor.moveToFirst();
		bundle.putString("0", m_RecCursor.getString(m_RecCursor.getColumnIndex("name")));
		bundle.putString("1", m_RecCursor.getString(m_RecCursor.getColumnIndex("sex")));
		bundle.putString("2", m_RecCursor.getString(m_RecCursor.getColumnIndex("nation")));
		bundle.putString("3",  m_RecCursor.getString(m_RecCursor.getColumnIndex("birth")));
		bundle.putString("4", m_RecCursor.getString(m_RecCursor.getColumnIndex("addr")));
		bundle.putString("5", m_RecCursor.getString(m_RecCursor.getColumnIndex("idnum")));	
		bundle.putString("6", m_RecCursor.getString(m_RecCursor.getColumnIndex("police")));	
		bundle.putString("7", m_RecCursor.getString(m_RecCursor.getColumnIndex("begin")));	
		bundle.putString("8", m_RecCursor.getString(m_RecCursor.getColumnIndex("end")));
		bundle.putString("9", m_RecCursor.getString(m_RecCursor.getColumnIndex("time")));   
		bundle.putByteArray("jpg", m_RecCursor.getBlob(m_RecCursor.getColumnIndex("jpg")));
		Display(bundle);
        
        Button btn;
        if (m_iRowNum == m_iTotalRec){
            btn = (Button)findViewById(R.id.nextpage);
            btn.setEnabled(false);         
            btn = (Button)findViewById(R.id.lastpage);
            btn.setEnabled(false);            	
        }
        
        if (m_iRowNum == 1){
            btn = (Button)findViewById(R.id.firstpage);
            btn.setEnabled(false);         
            btn = (Button)findViewById(R.id.prepage);
            btn.setEnabled(false);            	
        }
        
        if (m_iRowNum > 1 && m_iTotalRec > 1){
            btn = (Button)findViewById(R.id.firstpage);
            btn.setEnabled(true);         
            btn = (Button)findViewById(R.id.prepage);
            btn.setEnabled(true);          	
        }
        
        if (m_iRowNum < m_iTotalRec && m_iTotalRec > 1){
            btn = (Button)findViewById(R.id.nextpage);
            btn.setEnabled(true);         
            btn = (Button)findViewById(R.id.lastpage);
            btn.setEnabled(true);            	
        }  
        
        TextView txt = (TextView)findViewById(R.id.totalpage);
        txt.setText("共" + m_iTotalRec + "条");
        
        EditText edit = (EditText)findViewById(R.id.curpage);
        if (m_iTotalRec == 0){
        	edit.setText("0");
        	txt.setText("共0条");
        }else{
        	edit.setText("" + (m_iRowNum));
        }   
        edit.setCursorVisible(false);
	}
    
    void DisplayWgr(Bundle bundle)
    { 	
    	TextView v = (TextView)findViewById(R.id.textView1); 
		v.setText("英文名 " + bundle.get("4") + "\r\n中文名 " + bundle.get("0"));        		        	     	

		v = (TextView)findViewById(R.id.textView2);
		String szTmp = bundle.getString("1");
		if (szTmp.compareToIgnoreCase("女") == 0){			
			szTmp = "女/F";
		}else{
			szTmp = "男/M";
		}
		v.setText("性别  " + szTmp);        	
						
		v = (TextView)findViewById(R.id.textView3);  
		szTmp = (String)bundle.get("3");
		szTmp = szTmp.substring(0, 4) + "."
			     + szTmp.substring(4, 6) + "."
				 + szTmp.substring(6, 8);		
		
		v.setText("出生  " + szTmp);   
		
		szTmp = bundle.getString("2");
		String[] s = GuoJiPair.GetGuoJi(szTmp);
		if (s != null){
			v = (TextView)findViewById(R.id.textView4); 
			v.setText("国籍 " + s[2] + "/" + s[1]);
		} 		
		
		v = (TextView)findViewById(R.id.textView5);       		
		v.setText("有效期限  " + (String)bundle.get("7") + "-" +  (String)bundle.get("8"));
				
		v = (TextView)findViewById(R.id.textView6);     		
		v.setText("签发机关  " + "公安部/Ministry of Public Security");
				
		v = (TextView)findViewById(R.id.textView7);  			
		v.setText("有效期限  " + (String)bundle.get("7") + "-" +  (String)bundle.get("8"));
		
		v = (TextView)findViewById(R.id.textView8);
		v.setText("公民身份证号  " + bundle.get("5"));
		
		v = (TextView)findViewById(R.id.textView9);	
		v.setText("读卡时间  " + (String)bundle.get("9"));  
		
		byte[] jpg = bundle.getByteArray("jpg");
		Bitmap bmp = BitmapFactory.decodeByteArray(jpg, 0, jpg.length);		

        ImageView iv = (ImageView)findViewById(R.id.imageView1); 
        iv.setImageBitmap(bmp);	 
    }
    
    void Display(Bundle bundle)
    { 	
    	byte c = bundle.getString("4").toLowerCase().getBytes()[0];
    	if ((c >= "a".getBytes()[0] && c <= "z".getBytes()[0])){
    		DisplayWgr(bundle);
    		return;
    	}
    	
		TextView v = (TextView)findViewById(R.id.textView1); 
		v.setText("姓名  " + bundle.get("0"));  
		
		v = (TextView)findViewById(R.id.textView2);
		v.setText("性别  " + bundle.get("1"));        	
		
		v = (TextView)findViewById(R.id.textView3);      		
		v.setText("民族  " + bundle.get("2"));     
				
		v = (TextView)findViewById(R.id.textView4);  
		String szTmp = (String)bundle.get("3");
		szTmp = szTmp.substring(0, 4) + "年"
			     + szTmp.substring(4, 6) + "月"
				 + szTmp.substring(6, 8) + "日";		
		
		v.setText("出生  " + szTmp);   
		
		v = (TextView)findViewById(R.id.textView5);       		
		v.setText("住址  " + (String)bundle.get("4"));    		
		
		v = (TextView)findViewById(R.id.textView6);       		
		v.setText("身份证号 " + (String)bundle.get("5"));
				
		v = (TextView)findViewById(R.id.textView7);     		
		v.setText("签发机关  " + (String)bundle.get("6"));
				
		v = (TextView)findViewById(R.id.textView8);  			
		v.setText("有效期限  " + (String)bundle.get("7") + "-" +  (String)bundle.get("8"));
		
		v = (TextView)findViewById(R.id.textView9);	
		v.setText("读卡时间  " + (String)bundle.get("9"));  
		
		byte[] jpg = bundle.getByteArray("jpg");
		Bitmap bmp = BitmapFactory.decodeByteArray(jpg, 0, jpg.length);		

        ImageView iv = (ImageView)findViewById(R.id.imageView1); 
        iv.setImageBitmap(bmp);	 
    }
    /*
    private void Display(Bundle bundle)
    {
		int id[] = {R.id.textName, R.id.textSex, R.id.textNation, R.id.textBirth, R.id.textBirth1, R.id.textBirth2, 
				R.id.textAddr, R.id.textId,R.id.textAppAddress,R.id.textFinger, R.id.textPolice, R.id.textLimit, R.id.textMsg}; 		
		TextView v;    
		for(int i=0; i<id.length; i++){
			v = (TextView)findViewById(id[i]);
			String str = "" + i;
			v.setText("" + bundle.get(str));
		}
		
		byte[] jpg = bundle.getByteArray("jpg");
		Bitmap bmp = BitmapFactory.decodeByteArray(jpg, 0, jpg.length);		

        ImageView iv = (ImageView)findViewById(R.id.CardHead); 
        iv.setImageBitmap(bmp);	 	
    }
    */
}
